package org.jtao.gbsubject.web.rest;

import com.codahale.metrics.annotation.Timed;
import org.jtao.gbsubject.service.IndicatorService;
import org.jtao.gbsubject.web.rest.errors.BadRequestAlertException;
import org.jtao.gbsubject.web.rest.util.HeaderUtil;
import org.jtao.gbsubject.web.rest.util.PaginationUtil;
import org.jtao.gbsubject.service.dto.IndicatorDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Indicator.
 */
@RestController
@RequestMapping("/api")
public class IndicatorResource {

    private final Logger log = LoggerFactory.getLogger(IndicatorResource.class);

    private static final String ENTITY_NAME = "indicator";

    private final IndicatorService indicatorService;

    public IndicatorResource(IndicatorService indicatorService) {
        this.indicatorService = indicatorService;
    }

    /**
     * POST  /indicators : Create a new indicator.
     *
     * @param indicatorDTO the indicatorDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new indicatorDTO, or with status 400 (Bad Request) if the indicator has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/indicators")
    @Timed
    public ResponseEntity<IndicatorDTO> createIndicator(@RequestBody IndicatorDTO indicatorDTO) throws URISyntaxException {
        log.debug("REST request to save Indicator : {}", indicatorDTO);
        if (indicatorDTO.getId() != null) {
            throw new BadRequestAlertException("A new indicator cannot already have an ID", ENTITY_NAME, "idexists");
        }
        IndicatorDTO result = indicatorService.save(indicatorDTO);
        return ResponseEntity.created(new URI("/api/indicators/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /indicators : Updates an existing indicator.
     *
     * @param indicatorDTO the indicatorDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated indicatorDTO,
     * or with status 400 (Bad Request) if the indicatorDTO is not valid,
     * or with status 500 (Internal Server Error) if the indicatorDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/indicators")
    @Timed
    public ResponseEntity<IndicatorDTO> updateIndicator(@RequestBody IndicatorDTO indicatorDTO) throws URISyntaxException {
        log.debug("REST request to update Indicator : {}", indicatorDTO);
        if (indicatorDTO.getId() == null) {
            return createIndicator(indicatorDTO);
        }
        IndicatorDTO result = indicatorService.save(indicatorDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, indicatorDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /indicators : get all the indicators.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of indicators in body
     */
    @GetMapping("/indicators")
    @Timed
    public ResponseEntity<List<IndicatorDTO>> getAllIndicators(Pageable pageable) {
        log.debug("REST request to get a page of Indicators");
        Page<IndicatorDTO> page = indicatorService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/indicators");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /indicators/:id : get the "id" indicator.
     *
     * @param id the id of the indicatorDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the indicatorDTO, or with status 404 (Not Found)
     */
    @GetMapping("/indicators/{id}")
    @Timed
    public ResponseEntity<IndicatorDTO> getIndicator(@PathVariable Long id) {
        log.debug("REST request to get Indicator : {}", id);
        IndicatorDTO indicatorDTO = indicatorService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(indicatorDTO));
    }

    /**
     * DELETE  /indicators/:id : delete the "id" indicator.
     *
     * @param id the id of the indicatorDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/indicators/{id}")
    @Timed
    public ResponseEntity<Void> deleteIndicator(@PathVariable Long id) {
        log.debug("REST request to delete Indicator : {}", id);
        indicatorService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
